﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace StudentInBinary
{
    class BinaryStorage
    {
        public static void AppendStudentToFile(Student st) 
        {
            if (!File.Exists("Students.sav")) 
            {
                using(File.Create("Students.sav"));
            }
            BinaryWriter ioFile = new BinaryWriter(File.Open("Students.sav", FileMode.Append));
            using (ioFile)
            {
                //BinaryFormatter form = new BinaryFormatter();
                //form.Serialize(inputFile, st);
                ioFile.Write(st.FirstName);
                ioFile.Write(st.LasName);
                ioFile.Write(st.FacNumber);
            }
        }

        public static void DeleteStudent(Student st) 
        {

        }

        public static void PrintAllStudents()
        {
            BinaryReader ioFile = new BinaryReader(File.Open("Students.sav", FileMode.Open));
            using (ioFile) 
            {
                bool endReaced = false;
                while (endReaced == false)
                {
                    try
                    {
                        string firstName = ioFile.ReadString();
                        string lastName = ioFile.ReadString();
                        int facNumber = ioFile.ReadInt32();
                        Console.WriteLine("First name: {0}\nLast name: {1} \nfacNumber:", firstName, lastName, facNumber);
                    }
                    catch (EndOfStreamException) 
                    {
                        endReaced = true;
                    }
                }
                
            }
        }

        static void Main()
        {
            Student st1 = new Student("pesho","Peshev",123);
            Student st2 = new Student("Qvor", "Tsvetkov", 456);
            Student st3 = new Student("Sasho", "Sashev", 789);
            AppendStudentToFile(st1);
            AppendStudentToFile(st2);
            AppendStudentToFile(st3);
            PrintAllStudents();
        }
    }
}
